package com.validator.plus.constraint;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class RangeEnumValidator implements ConstraintValidator<RangeEnum, Object> {
    private RangeEnum range;
    @Override
    public void initialize(RangeEnum constraintAnnotation) {
        this.range = constraintAnnotation;
    }
    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        String[] allowableValues = range.allowableValues();
        if (null == o || allowableValues.length == 0) {
            return false;
        }
        String s = o.toString();
        for (String allowableValue : allowableValues) {
            if (s.equals(allowableValue)) {
                return true;
            }
        }
        return false;
    }
}